package edu.whu.webgis.controller;

import edu.whu.webgis.entity.Comment;
import edu.whu.webgis.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    ICommentService service;

    @GetMapping("/get")
    public ResponseEntity<List<Comment>> getCommentsByPid(@RequestParam Integer pid){
        List<Comment> comments = service.getCommentsByPid(pid);
        return ResponseEntity.ok(comments);
    }

    @PostMapping("/insert")
    public ResponseEntity<Object> insertComment(@RequestBody Comment comment){
        int count = service.insertComment(comment);
        if (count == 0){
            return ResponseEntity.badRequest().build();
        }
        else {
            return ResponseEntity.ok().build();
        }
    }
}
